package com.itboye.sunsunhome.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsunhome.bean.PersonDataBean;
import com.itboye.sunsunhome.util.HttpRequest;
import com.itboye.sunsunhome.util.SPContants;
import com.itboye.sunsunhome.volley.CodeErrorException;
import com.itboye.sunsunhome.volley.MyJsonRequest;
import com.itboye.sunsunhome.volley.XErrorListener;
import com.itboye.sunsunhome.volley.XRequestListener;
import com.itboye.sunsunhome.www.aq.R;
import com.itboye.sunsunhome.www.aq.base.MyApplication;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

	private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;

	private IWXAPI api;
	private String code;

	public static String DISMISSFRAGMENT = "com.itboye.sunsun.dismiss";
	public static String NotifyPerson = "com.itboye.sunsun.notify.person";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.entiey);

		api = WXAPIFactory.createWXAPI(this, "wxabf9bc3a048000ac", false);

		api.handleIntent(getIntent(), this);
		SendAuth.Resp resp = new SendAuth.Resp(getIntent().getExtras());
		code = resp.code;
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		setIntent(intent);
		api.handleIntent(intent, this);

	}

	@Override
	public void onReq(BaseReq req) {

		WXEntryActivity.this.finish();

	}

	@Override
	public void onResp(BaseResp resp) {

		this.finish();

		int result = 0;

		code = ((SendAuth.Resp) resp).code;
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			result = R.string.entity;

			if (resp.transaction.equals("sessionwebpage")) {
				Toast.makeText(MyApplication.context, "分享成功", 0).show();
				Intent intent = new Intent(DISMISSFRAGMENT);
				sendBroadcast(intent);
				this.finish();

			}
			if (resp.transaction.equals("webpage")) {
				Toast.makeText(MyApplication.context, "分享成功", 0).show();
				// Intent intent=new Intent(DISMISSFRAGMENT);
				// sendBroadcast(intent);
			}
			// 如果是微信登录
			if (resp.transaction.equals("login")) {
				Intent intent=new Intent("AQLoginAQLogin");
				intent.putExtra("weixinlofin",code);
				
				System.out.println("AQLoginAQLogin"+code);
				sendBroadcast(intent);
				
			}
			// 如果是绑定手机
			else if (resp.transaction.equals("bind")) {
				Intent intent=new Intent("bangidng");
				intent.putExtra("bangidng",code);
				
				System.out.println("bangidng"+code);
				sendBroadcast(intent);
				WXEntryActivity.this.finish();
			}

			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result = R.string.errcode_cancel;
			WXEntryActivity.this.finish();
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = R.string.errcode_deny;
			WXEntryActivity.this.finish();
			break;
		default:
			result = R.string.errcode_unknown;
			WXEntryActivity.this.finish();
			break;
		}

		Toast.makeText(this, result, Toast.LENGTH_LONG).show();
	}

	

}